package com.crm.controller;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.crm.entity.User;
import com.crm.service.UserRolesService;
import com.crm.service.UserService;
import com.crm.utils.DateUtil;
import com.crm.utils.PageUtil;
import com.crm.utils.ResponeMessageUtil;
import com.crm.utils.UUIDUtil;

@RequestMapping("/user")
@RestController
public class UserController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private UserRolesService userRolesService;
	
	@RequestMapping("/loginNameAndPwd")
	public ModelAndView loginNameAndPwd(String LoginName, String Password) {
		User user = null;
		ModelAndView modelAndView = new ModelAndView();
		user = userService.selectByName(LoginName);
		if (user != null) {
			user = userService.selectByNameAndPwd(LoginName, Password);
			if (user != null) {
				modelAndView.setViewName("redirect:/main.html");
		        return modelAndView;

			}
			modelAndView.setViewName("redirect:/login.html");
	        return modelAndView;

		}
		modelAndView.setViewName("redirect:/login.html");
        return modelAndView;
	}
	
	//判断用户名是否重复
	@RequestMapping("/selectRepetition")
	public boolean selectRepetition(String loginName) {
		//String loginName = user.getLoginName();
		User userList = userService.isRepetitionUser(loginName);
		if (userList == null) {
			return true;//返回true则为没有该用户名可以被注册
	    }else {
	        return false;
		}
	}
	
	@RequestMapping("/GetUsersLayUI")
	public ResponeMessageUtil GetUsersLayUI(Integer limit,Integer page,String loginName, String createTime, String endDate, String isLockout, String orderBy) {
		Integer setValue = PageUtil.getSetValue(page, limit);
		return ResponeMessageUtil.ok(userService.getCount(loginName, createTime, endDate, isLockout, orderBy), userService.selectAll(setValue, limit, loginName, createTime, endDate, isLockout, orderBy));
	}
	
	@RequestMapping("/CreateUser")
	public boolean CreateUser(User user) {
		user.setId(UUIDUtil.getUUID());
		user.setCreateTime(DateUtil.dateToString(new Date()));
		return userService.insertSelective(user)>0?true:false;
	}
	
	@RequestMapping("/UpdateUser")
	public boolean UpdateUser(User user) {
		return userService.updateByPrimaryKeySelective(user)>0?true:false;
	}
	
	@RequestMapping("/DeleteUser")
	public boolean DeleteUser(String id) {
		return userService.deleteByPrimaryKey(id)>0?true:false;
	}
	
	@RequestMapping("/ResetPassword")
	public boolean ResetPassword(User user) {
		user.setPassword("123456");
		return userService.updateByPrimaryKeySelective(user)>0?true:false;
	}
	
	@RequestMapping("/LockUser")
	public boolean LockUser(String id,String valid) {
		String isLockout = "";
		if (valid != null && valid != "" && valid.equals("on")) {
			isLockout = "是";
		} else {
			isLockout = "否";
		}
		return userService.updateByIsLockout(id,isLockout)>0?true:false;
	}
	
	@RequestMapping("/UnLockUser")
	public boolean UnLockUser(String id,String valid) {
		String isLockout = "";
		if (valid != null && valid != "" && valid.equals("on")) {
			isLockout = "是";
		} else {
			isLockout = "否";
		}
		return userService.updateByIsLockout(id,isLockout)>0?true:false;
	}
	
	
	//删除用户角色
	@RequestMapping("/RemoveUserFromRole")
	public boolean RemoveUserFromRole(String userid,String roleid) {
		return userRolesService.deleteByUidAndRoleid(userid,roleid)>0?true:false;
	} 
	
	//增加用户角色
	@RequestMapping("/AddUserToRole")
	public boolean AddUserToRole(String userid,String roleid) {
		String id = UUIDUtil.getUUID();
		return userRolesService.insertByUidAndRoleid(id,userid,roleid)>0?true:false;
	}

}